package com.woniu.springboot.controller;

import com.woniu.springboot.entity.Department;
import com.woniu.springboot.entity.Employee;
import com.woniu.springboot.service.IDepartmentService;
import com.woniu.springboot.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Controller
public class EmpController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String list(String ename, Map map){
        Collection<Employee> lists = employeeService.findEmployeeAll();
        map.put("lists",lists);
        return "list";
    }

    @RequestMapping("/search")
    public String search(String searchName, Map map){
        List<Employee> name = employeeService.findEmployeeByName(searchName);
        map.put("lists",name);
        map.put("searchName",searchName);
        return "list";

    }

    @RequestMapping("/addView")
    public String addView(Map map){
        Collection<Department> departments = departmentService.findDepartmentAll();
        map.put("depts",departments);
        return "add";
    }

    @RequestMapping("/save")
    public String save(Employee employee){
        System.out.println(employee);
        employeeService.addEmployee(employee);
        return "redirect:list";
    }

    @RequestMapping("/del")
    public String remove(Integer id){
        employeeService.removeEmployeeByKey(id);
        System.out.println(id);
        return "redirect:list";
    }


    @RequestMapping("/modView")
    public String modView(Map map, Integer id, HttpSession session){
        session.setAttribute("id",id);
        System.out.println(id);
        Collection<Department> departments = departmentService.findDepartmentAll();
        map.put("depts",departments);
        return "modify";
    }
    @RequestMapping("/mod")
    public String modify(Employee employee, HttpServletRequest request){
        //System.out.println(request.getSession().getAttribute("id"));
        employee.setId((Integer) request.getSession().getAttribute("id"));
        System.out.println(employee);
        employeeService.modifyEmployeeByKey(employee);
        return "redirect:list";
    }
}
